package fr.utbm.aiechat.activities;

import fr.utbm.aiechat.localisation.*;
import fr.utbm.aiechat.structures.*;
import fr.utbm.aiechat.LocalisationData;
import fr.utbm.aiechat.Network;
import fr.utbm.aiechat.R;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;

public class MapViewActivity extends MapActivity{
	private LocationManager lm;
	private double      lat = 0;
	private double      lng = 0;
	private MapController mc;
	
	private GeoPoint ownPoint;
	private MarkerList defaultList, menList, womenList;
	private List<Overlay> mapOverlays;
	private  Drawable menPicture, womenPicture, phonePicture;
	private Bundle b;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview_layout);
        
        //get mapview for layout
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        
        //map controller --> to auto-center of the user's position
        mc = mapView.getController();
        mc.setZoom(15);
        
        //creation pictures
        menPicture = this.getResources().getDrawable(R.drawable.men);
        womenPicture = this.getResources().getDrawable(R.drawable.women);
        phonePicture = this.getResources().getDrawable(R.drawable.phone);
        
        //creation markers list
        mapOverlays = mapView.getOverlays();
        defaultList = new MarkerList(phonePicture, this);
        menList = new MarkerList(menPicture, this);
        womenList = new MarkerList(womenPicture, this);
        
        b = getIntent().getExtras();
        
		if(b.getString("typeView").equals("oneUser")){
			System.out.println("display one user");
        	int idUser = b.getInt("idUser");
        	UserAdapter Users = Network.getInstance().Users;
    		lat = Users.getItem(idUser).getLat();
    		lng = Users.getItem(idUser).getLng();
    		GeoPoint pointUser = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
    		OverlayItem overlayTemp = new OverlayItem(pointUser, Users.getItem(idUser).getName(), "");
    		if(Users.getItem(idUser).getSexe().equals("Homme")){
    			menList.addOverlay(overlayTemp);
    		}
    		else{
    			womenList.addOverlay(overlayTemp);
    		}
    		mc.animateTo(pointUser);
    		mc.setCenter(pointUser);
        }
        else{
	        UserAdapter Users = Network.getInstance().Users;
	        System.out.println("display all users");
	        for(int i=0; i<Users.getCount(); i++){
	        	if(Users.getItem(i).getLat() != 0 && Users.getItem(i).getLng() != 0){
		        	GeoPoint pointTemp = new GeoPoint((int) (Users.getItem(i).getLat() * 1E6), (int) (Users.getItem(i).getLng() * 1E6));
		            OverlayItem overlayTemp = new OverlayItem(pointTemp, Users.getItem(i).getName(), "");
		            if(Users.getItem(i).getSexe().equals("Homme")){
		            	menList.addOverlay(overlayTemp);
		            }
		            else{
		            	womenList.addOverlay(overlayTemp);
		            }
	        	}				
	        }
	        
	        //own position
	        lat = LocalisationData.getInstance().getLat();
			lng = LocalisationData.getInstance().getLng();
			if(lat != 0 && lng != 0){
				ownPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
				OverlayItem overlayitemBelfort = new OverlayItem(ownPoint, "Ma position", "");
				defaultList.addOverlay(overlayitemBelfort);
				mapOverlays.add(defaultList);
				mc.animateTo(ownPoint);
				mc.setCenter(ownPoint);
			}
        }
        
		if(menList.size() != 0){
			mapOverlays.add(menList);
		}
		if(womenList.size() != 0){
			mapOverlays.add(womenList);
		}
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}